package com.example.shop.goods.dto;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.goods.enums.ShelfState;
import com.example.shop.pojo.Goods;
import com.example.shop.pojo.GoodsType;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.elasticsearch.annotations.Document;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "goods-detail-browse-info",createIndex = false)
public class MerchantGoodsDetailBrowseInfo extends GoodsBrowseInfo{
    /**
     * 商品的介绍
     */
    protected String goodsIntroduction;
    /**
     * 商品的视频介绍
     */
    protected String goodsVideo;
    /**
     * 商品详细图片,比如：价值、效果、保证等
     */
    protected List<String> goodsDetailsPicture;
    /**
     * 该商品的好评数
     */
    protected int favorableCommentNumber;
    /**
     * 该商品的中评数
     */
    protected int mediumCommentNumber;
    /**
     * 该商品的差评数
     */
    protected int badCommentNumber;
    /**
     * 该商品的上架时间
     */
    protected LocalDateTime shelfTime;
    /**
     * 该商品的上架状态
     */
    protected ShelfState shelfState;
    /**
     * 评价总数：由  好评数+中评数+差评数
     */
    protected int evaluationTotalNum;
    /**
     * 好评率：由  好评数/评价数
     */
    protected float favorableRating;

    public MerchantGoodsDetailBrowseInfo setGoodsVideo(String goodsVideo) {
        this.goodsVideo = ResponseResult.toRequestPath(goodsVideo);
        return this;
    }

    public MerchantGoodsDetailBrowseInfo setGoodsDetailsPicture(List<String> goodsDetailsPicture) {
        ArrayList<String> requestList = new ArrayList<>();
        for(String resource:goodsDetailsPicture){
            requestList.add(ResponseResult.toRequestPath(resource));
        }
        this.goodsDetailsPicture = requestList;
        return this;
    }

    public MerchantGoodsDetailBrowseInfo setFavorableCommentNumber(Integer favorableCommentNumber) {
        this.favorableCommentNumber = favorableCommentNumber;
        compute();
        return this;
    }

    public MerchantGoodsDetailBrowseInfo setMediumCommentNumber(Integer mediumCommentNumber) {
        this.mediumCommentNumber = mediumCommentNumber;
        compute();
        return this;
    }

    public MerchantGoodsDetailBrowseInfo setBadCommentNumber(Integer badCommentNumber) {
        this.badCommentNumber = badCommentNumber;
        compute();
        return this;
    }

    private void compute(){
        evaluationTotalNum =  this.favorableCommentNumber + this.mediumCommentNumber + this.badCommentNumber;
        if(evaluationTotalNum!=0){
            //好評率等于好评率/评价总数
            favorableRating = this.favorableCommentNumber/evaluationTotalNum;
        }
    }

    @Override
    public String toString() {
        return "MerchantGoodsDetailBrowseInfo{" +
                super.toString() +
                "goodsIntroduction='" + goodsIntroduction + '\'' +
                ", goodsVideo='" + goodsVideo + '\'' +
                ", goodsDetailsPicture=" + goodsDetailsPicture +
                ", favorableCommentNumber=" + favorableCommentNumber +
                ", mediumCommentNumber=" + mediumCommentNumber +
                ", badCommentNumber=" + badCommentNumber +
                ", shelfTime=" + shelfTime +
                ", shelfState=" + shelfState +
                ", evaluationTotalNum=" + evaluationTotalNum +
                ", favorableRating=" + favorableRating +
                '}';
    }
    public MerchantGoodsDetailBrowseInfo assignment(Goods goods, List<String> descriptionPicture, List<String> detailsPicture, List<GoodsStyle> goodsStyleList){
        super.assignment(goods,descriptionPicture,goodsStyleList);
        this.setGoodsIntroduction(goods.getGoodsIntroduction())
                .setGoodsVideo(goods.getGoodsVideo())
                .setGoodsDetailsPicture(detailsPicture)
                .setFavorableCommentNumber(goods.getFavorableCommentNumber())
                .setMediumCommentNumber(goods.getMediumCommentNumber())
                .setBadCommentNumber(goods.getBadCommentNumber())
                .setShelfTime(goods.getShelfTime())
                .setShelfState(ShelfState.getShelfState(goods.getShelfState()));
        return this;
    }
}
